﻿using System;
using System.Linq;

namespace Solution.BusinessObject
{
    /// <summary>
    /// Provides methods for manipulating <see cref="Language"/> entity.
    /// </summary>
    public class LanguageBO
    {
        /// <summary>
        /// Gets all availables menu item.
        /// </summary>
        public Category[] GetMainMenu(Language language)
        {
            return CommonBO.MasterEDM.Categories
                .Where(t => t.Language == language)
                .OrderBy(t=>t.DisplayOrder)
                .ToArray();
        }

        /// <summary>
        /// Get all availabe language on site.
        /// </summary>
        public Language[] GetLanguages()
        {
            return CommonBO.MasterEDM.Languages
                .Where(t => t.Enabled == true)
                .OrderBy(t => t.DisplayOrder)
                .ToArray();
        }

        /// <summary>
        /// Get language by language code, if not found, return default language.
        /// </summary>
        public Language GetLanguage(string languageCode)
        {
            // if invalid language code
            if (String.IsNullOrEmpty(languageCode))
            {
                // return defaul
                return CommonBO.DefaultSiteLanguage;
            }

            // if not found the specific
            var language = CommonBO.MasterEDM.Languages
                               .FirstOrDefault(t => t.Code == languageCode)
                           ?? CommonBO.DefaultSiteLanguage;

            return language;
        }
    }
}